package jinyilw.swing.rlaf.ui;

import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicScrollPaneUI;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Jinyi
 */
public class RScrollPaneUI extends BasicScrollPaneUI
{
	private RFocusManager focusManager;
	private final RenderingConfig renderingConfig = new RenderingConfig();

	public static ComponentUI createUI(JComponent c)
	{
		return new RScrollPaneUI();
	}

	public void setFocusManager(RFocusManager focusManager)
	{
		this.focusManager = focusManager;
	}

	@Override
	protected void installDefaults(JScrollPane scrollPane)
	{
		Border vpBorder = scrollPane.getViewportBorder();
		if ((vpBorder == null) || (vpBorder instanceof UIResource))
		{
			vpBorder = UIManager.getBorder("ScrollPane.viewportBorder");
			scrollPane.setViewportBorder(vpBorder);
		}
		scrollPane.setOpaque(false);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		if (focusManager != null && focusManager.isEnable())
		{
			Graphics2D g2d = (Graphics2D) g;
			renderingConfig.setAAPainting(g2d);
			if (focusManager.isFocus)
				g.setColor(RThemeColor.themeColor);
			else
				g.setColor(RThemeColor.colorSet.borderColor);
			g.drawRoundRect(0, 1, c.getWidth() - 1, c.getHeight() - 2, 5, 5);
			renderingConfig.resetAAPainting(g2d);
		}
	}

	@Override
	public void paint(Graphics g, JComponent c)
	{

	}
}
